Objects leaking immediately from allocation using either new or [[Object alloc] init];

Posted by Sam on Stack Overflow See other posts from Stack Overflow or by Sam
Published on 2010-03-18T09:28:48Z Indexed on 2010/03/18 9:31 UTC
Read the original article Hit count: 153

While running Instruments to find leaks in my code, after I've loaded a file and populate an NSMutableArray with new objects, leaks pop up! I am correctly releasing the objects. Sample code below:

    //NSMutableArray declared as a retained property in the parent class
if(!mutableArray)
    mutableArray = [[NSMutableArray alloc] initWithCapacity:objectCount];
else
    [mutableArray removeAllObjects];

//Iterates through the read in data and populate the NSMutableArray
for(int i = 0; i < objectCount; i++){

//Initializes a new object with data
    MyObject *object = [MyObject new];

//Adds the object to the mutableArray
    [mutableArray addObject:object];

//Releases the object
    [object release];
}

I get a number of leaks from Instruments terminating at the addition of the 'object' into the 'mutableArray', but also including the allocation of the 'object' and the 'mutableArray'.

I don't get it. Not to mention, this is happening on the first call of the enclosing method so the allocation of the NSMutableArray is being hit in the logic block, not the 'removeAllObjects' selector.

Lastly, does Core Foundation have a major bug in it that randomly creates CFStrings and mismanages their memory? My code does not even use those, nor do the leaks where they occur have anything to do with my code. Almost all of my applications so far deal with OpenGL (in case anyone knows of a threading issue that arises from trying to synch the backend of the program with the front end of displaying the contents of an NSOpenGLView class or whatever it is).

© Stack Overflow or respective owner

Related posts about nsmutablearray

Related posts about cfstring